package com.atxyj.springsecurity.security.exception;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xieyujiao
 * @description 解决匿名用户访问需要权限才能访问资源时的异常
 * @date 2021/7/30
 */
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {


    /**
     * 当用户尝试访问需要权限才能访问的rest资源而不需要提供token或者token错误或过期时，
     * 将调用方法发送401相应以及错误信息
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
       response.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
    }
}
